home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Applications 2004 May / SGI IRIX 6.5 Applications 2004 May.iso / dist / java3d.idb / usr / demos / java / j3d / programs / examples / TextureByReference / TextureByReference.java.z / TextureByReference.java
Encoding:
Java Source  |  2003-08-08  |  16.7 KB  |  526 lines

  1. /*
  2.  *    @(#)TextureByReference.java 1.14 02/10/21 14:36:22
  3.  *
  4.  * Copyright (c) 1996-2002 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  *
  10.  * - Redistributions of source code must retain the above copyright
  11.  *   notice, this list of conditions and the following disclaimer.
  12.  *
  13.  * - Redistribution in binary form must reproduce the above copyright
  14.  *   notice, this list of conditions and the following disclaimer in
  15.  *   the documentation and/or other materials provided with the
  16.  *   distribution.
  17.  *
  18.  * Neither the name of Sun Microsystems, Inc. or the names of
  19.  * contributors may be used to endorse or promote products derived
  20.  * from this software without specific prior written permission.
  21.  *
  22.  * This software is provided "AS IS," without a warranty of any
  23.  * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
  24.  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
  25.  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
  26.  * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
  27.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  28.  * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
  29.  * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
  30.  * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
  31.  * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
  32.  * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
  33.  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  34.  *
  35.  * You acknowledge that Software is not designed,licensed or intended
  36.  * for use in the design, construction, operation or maintenance of
  37.  * any nuclear facility.
  38.  */
  39.  
  40. import java.applet.Applet;
  41. import java.awt.*;
  42. import java.awt.event.*;
  43. import com.sun.j3d.utils.applet.MainFrame;
  44. import com.sun.j3d.utils.universe.*;
  45. import javax.media.j3d.*;
  46. import javax.vecmath.*;
  47. import java.awt.image.*;
  48. import java.awt.color.ColorSpace;
  49. import com.sun.j3d.utils.image.TextureLoader;
  50. import javax.swing.*;
  51. import javax.swing.event.*;
  52.  
  53.  
  54. public class TextureByReference extends Applet 
  55. implements ItemListener, ActionListener, ChangeListener {
  56.  
  57.   // need reference to animation behavior
  58.   private AnimateTexturesBehavior animate;
  59.  
  60.   // need reference to tetrahedron
  61.   private Tetrahedron tetra;
  62.   
  63.   // the gui buttons
  64.    private JCheckBox flipB;
  65.   private JRadioButton texByRef;
  66.   private JRadioButton texByCopy;
  67.   private JRadioButton geomByRef;
  68.   private JRadioButton geomByCopy;
  69.   private JRadioButton img4ByteABGR;
  70.   private JRadioButton img3ByteBGR;
  71.   private JRadioButton imgIntARGB;
  72.   private JRadioButton imgCustomRGBA;
  73.   private JRadioButton imgCustomRGB;
  74.   private JRadioButton yUp;
  75.   private JRadioButton yDown;
  76.   private JButton animationB;
  77.   private JSlider frameDelay;
  78.  
  79.     private SimpleUniverse universe = null;
  80.  
  81.   // image files used for the Texture animation for the applet,
  82.   // or if no parameters are passed in for the application
  83.   public static final String[] defaultFiles = {
  84.     "../images/animation1.gif",
  85.     "../images/animation2.gif",
  86.     "../images/animation3.gif",
  87.     "../images/animation4.gif",
  88.     "../images/animation5.gif",
  89.     "../images/animation6.gif",
  90.     "../images/animation7.gif",
  91.     "../images/animation8.gif",
  92.     "../images/animation9.gif",
  93.     "../images/animation10.gif"};
  94.  
  95.   private java.net.URL[] urls = null;
  96.  
  97.   
  98.   public TextureByReference() {
  99.   }
  100.  
  101.    public TextureByReference(java.net.URL[] fnamesP) {
  102.      urls = fnamesP;
  103.    }
  104.  
  105.   public void init() {
  106.     if (urls == null) {
  107.       urls = new java.net.URL[defaultFiles.length];
  108.       for (int i = 0; i < defaultFiles.length; i++) {
  109.     try {
  110.       urls[i] = new java.net.URL(getCodeBase().toString() + 
  111.                        defaultFiles[i]);
  112.     }
  113.     catch (java.net.MalformedURLException ex) {
  114.       System.out.println(ex.getMessage());
  115.       System.exit(1);
  116.     }
  117.       }
  118.     }
  119.     setLayout(new BorderLayout());
  120.     GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
  121.  
  122.     Canvas3D canvas = new Canvas3D(config);
  123.  
  124.     add("Center", canvas);
  125.  
  126.     // create a simple scene graph and attach it to a simple universe
  127.     BranchGroup scene = createSceneGraph();
  128.     universe = new SimpleUniverse(canvas);
  129.     universe.getViewingPlatform().setNominalViewingTransform();
  130.     universe.addBranchGraph(scene);
  131.  
  132.     // create the gui
  133.     JPanel gui = buildGui();
  134.     
  135.     this.add("South", gui);
  136.   }
  137.  
  138.     public void destroy() {
  139.     universe.cleanup();
  140.     }
  141.  
  142.   public JPanel buildGui() {
  143.     flipB = new JCheckBox("flip image", true);
  144.     flipB.addItemListener(this);
  145.     javax.swing.Box flipBox = new javax.swing.Box(BoxLayout.Y_AXIS);
  146.     flipBox.add(flipB);
  147.     Component strut1 = flipBox.createVerticalStrut(flipB.getPreferredSize().height);
  148.     Component strut2 = flipBox.createVerticalStrut(flipB.getPreferredSize().height);
  149.     Component strut3 = flipBox.createVerticalStrut(flipB.getPreferredSize().height);
  150.     Component strut4 = flipBox.createVerticalStrut(flipB.getPreferredSize().height);
  151.     Component strut5 = flipBox.createVerticalStrut(flipB.getPreferredSize().height);
  152.     flipBox.add(strut1);
  153.     flipBox.add(strut2);
  154.     flipBox.add(strut3);
  155.     flipBox.add(strut4);
  156.     flipBox.add(strut5);
  157.  
  158.     yUp = new JRadioButton("y up");
  159.     yUp.addActionListener(this);
  160.     yUp.setSelected(true);
  161.     yDown = new JRadioButton("y down");
  162.     yDown.addActionListener(this);
  163.     ButtonGroup yGroup = new ButtonGroup();
  164.     yGroup.add(yUp);
  165.     yGroup.add(yDown);
  166.     JLabel yLabel = new JLabel("Image Orientation:");
  167.     javax.swing.Box yBox = new javax.swing.Box(BoxLayout.Y_AXIS);
  168.     yBox.add(yLabel);
  169.     yBox.add(yUp);
  170.     yBox.add(yDown);
  171.     strut1 = yBox.createVerticalStrut(yUp.getPreferredSize().height);
  172.     strut2 = yBox.createVerticalStrut(yUp.getPreferredSize().height);
  173.     strut3 = yBox.createVerticalStrut(yUp.getPreferredSize().height);
  174.     yBox.add(strut1);
  175.     yBox.add(strut2);
  176.     yBox.add(strut3);
  177.  
  178.     texByRef = new JRadioButton("by reference");
  179.     texByRef.addActionListener(this);
  180.     texByRef.setSelected(true);
  181.     texByCopy = new JRadioButton("by copy");
  182.     texByCopy.addActionListener(this);
  183.     ButtonGroup texGroup = new ButtonGroup();
  184.     texGroup.add(texByRef);
  185.     texGroup.add(texByCopy);
  186.     JLabel texLabel = new JLabel("Texture:*");
  187.     javax.swing.Box texBox = new javax.swing.Box(BoxLayout.Y_AXIS);
  188.     texBox.add(texLabel);
  189.     texBox.add(texByRef);
  190.     texBox.add(texByCopy);
  191.     strut1 = texBox.createVerticalStrut(texByRef.getPreferredSize().height);
  192.     strut2 = texBox.createVerticalStrut(texByRef.getPreferredSize().height);
  193.     strut3 = texBox.createVerticalStrut(texByRef.getPreferredSize().height);
  194.     texBox.add(strut1);
  195.     texBox.add(strut2);
  196.     texBox.add(strut3);
  197.  
  198.     geomByRef = new JRadioButton("by reference");
  199.     geomByRef.addActionListener(this);
  200.     geomByRef.setSelected(true);
  201.     geomByCopy = new JRadioButton("by copy");
  202.     geomByCopy.addActionListener(this);
  203.     ButtonGroup geomGroup = new ButtonGroup();
  204.     geomGroup.add(geomByRef);
  205.     geomGroup.add(geomByCopy);
  206.     JLabel geomLabel = new JLabel("Geometry:");
  207.     javax.swing.Box geomBox = new javax.swing.Box(BoxLayout.Y_AXIS);
  208.     geomBox.add(geomLabel);
  209.     geomBox.add(geomByRef);
  210.     geomBox.add(geomByCopy);
  211.     strut1 = geomBox.createVerticalStrut(geomByRef.getPreferredSize().height);
  212.     strut2 = geomBox.createVerticalStrut(geomByRef.getPreferredSize().height);
  213.     strut3 = geomBox.createVerticalStrut(geomByRef.getPreferredSize().height);
  214.     geomBox.add(strut1);
  215.     geomBox.add(strut2);
  216.     geomBox.add(strut3);
  217.     
  218.     img4ByteABGR = new JRadioButton("TYPE_4BYTE_ABGR");
  219.     img4ByteABGR.addActionListener(this);
  220.     img4ByteABGR.setSelected(true);
  221.     img3ByteBGR = new JRadioButton("TYPE_3BYTE_BGR");
  222.     img3ByteBGR.addActionListener(this);
  223.     imgIntARGB = new JRadioButton("TYPE_INT_ARGB");
  224.     imgIntARGB.addActionListener(this);
  225.     imgCustomRGBA = new JRadioButton("TYPE_CUSTOM RGBA");
  226.     imgCustomRGBA.addActionListener(this);
  227.     imgCustomRGB = new JRadioButton("TYPE_CUSTOM RGB");
  228.     imgCustomRGB.addActionListener(this);
  229.     ButtonGroup imgGroup = new ButtonGroup();
  230.     imgGroup.add(img4ByteABGR);
  231.     imgGroup.add(img3ByteBGR);
  232.     imgGroup.add(imgIntARGB);
  233.     imgGroup.add(imgCustomRGBA);
  234.     imgGroup.add(imgCustomRGB);
  235.     JLabel imgLabel = new JLabel("Image Type:*");
  236.     javax.swing.Box imgBox = new javax.swing.Box(BoxLayout.Y_AXIS);
  237.     imgBox.add(imgLabel);
  238.     imgBox.add(img4ByteABGR);
  239.     imgBox.add(img3ByteBGR);
  240.     imgBox.add(imgIntARGB);
  241.     imgBox.add(imgCustomRGBA);
  242.     imgBox.add(imgCustomRGB);
  243.  
  244.     javax.swing.Box topBox = new javax.swing.Box(BoxLayout.X_AXIS);
  245.     topBox.add(flipBox);
  246.     topBox.add(texBox);
  247.     topBox.add(geomBox);
  248.     topBox.add(yBox);
  249.     Component strut = topBox.createRigidArea(new Dimension(10, 10));
  250.     topBox.add(strut);
  251.     topBox.add(imgBox);
  252.  
  253.     frameDelay = new JSlider(0, 50, 0);
  254.     frameDelay.addChangeListener(this);
  255.     frameDelay.setSnapToTicks(true);
  256.     frameDelay.setPaintTicks(true);
  257.     frameDelay.setPaintLabels(true);
  258.     frameDelay.setMajorTickSpacing(10);
  259.     frameDelay.setMinorTickSpacing(1);
  260.     frameDelay.setValue(20);
  261.     JLabel delayL = new JLabel("frame delay");
  262.     javax.swing.Box delayBox = new javax.swing.Box(BoxLayout.X_AXIS);
  263.     delayBox.add(delayL);
  264.     delayBox.add(frameDelay);
  265.     
  266.     animationB = new JButton(" stop animation ");
  267.     animationB.addActionListener(this);
  268.  
  269.     JLabel texInfo1 = new JLabel("*To use ImageComponent by reference feature, use TYPE_4BYTE_ABGR on Solaris");
  270.     JLabel texInfo2 = new JLabel("and TYPE_3BYTE_BGR on Windows");
  271.  
  272.     JPanel buttonP = new JPanel();
  273.     GridBagLayout gridbag = new GridBagLayout();
  274.     GridBagConstraints c = new GridBagConstraints();
  275.     buttonP.setLayout(gridbag);
  276.     c.anchor = GridBagConstraints.CENTER;
  277.     c.gridwidth = GridBagConstraints.REMAINDER;
  278.     gridbag.setConstraints(topBox, c);
  279.     buttonP.add(topBox);
  280.     gridbag.setConstraints(delayBox, c);
  281.     buttonP.add(delayBox);
  282.     gridbag.setConstraints(animationB, c);
  283.     buttonP.add(animationB);
  284.     gridbag.setConstraints(texInfo1, c);
  285.     buttonP.add(texInfo1);
  286.     gridbag.setConstraints(texInfo2, c);
  287.     buttonP.add(texInfo2);
  288.  
  289.     return buttonP;
  290.  
  291.   }
  292.  
  293.   public BranchGroup createSceneGraph() {
  294.  
  295.     // create the root of the branch group
  296.     BranchGroup objRoot = new BranchGroup();
  297.  
  298.     // create the transform group node and initialize it
  299.     // enable the TRANSFORM_WRITE capability so that it can be modified
  300.     // at runtime.  Add it to the root of the subgraph
  301.     Transform3D rotate = new Transform3D();
  302.     TransformGroup objTrans = new TransformGroup(rotate);
  303.     objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  304.     objRoot.addChild(objTrans);
  305.  
  306.     // bounds
  307.     BoundingSphere bounds =
  308.       new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
  309.  
  310.     // set up some light
  311.     Color3f lColor1 = new Color3f(0.7f, 0.7f, 0.7f);
  312.     Vector3f lDir1  = new Vector3f(-1.0f, -0.5f, -1.0f);
  313.     Color3f alColor = new Color3f(0.2f, 0.2f, 0.2f);
  314.     
  315.     AmbientLight aLgt = new AmbientLight(alColor);
  316.     aLgt.setInfluencingBounds(bounds);
  317.     DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1);
  318.     lgt1.setInfluencingBounds(bounds);
  319.     objRoot.addChild(aLgt);
  320.     objRoot.addChild(lgt1);
  321.     
  322.  
  323.     Appearance appearance = new Appearance();
  324.  
  325.     // enable the TEXTURE_WRITE so we can modify it at runtime
  326.     appearance.setCapability(Appearance.ALLOW_TEXTURE_WRITE);
  327.  
  328.     // load the first texture
  329.     TextureLoader loader = new TextureLoader(urls[0],
  330.                          TextureLoader.BY_REFERENCE |
  331.                           TextureLoader.Y_UP,
  332.                           this);
  333.     // get the texture from the loader
  334.     Texture2D tex = (Texture2D)loader.getTexture();
  335.     
  336.     // get the BufferedImage to convert to TYPE_4BYTE_ABGR and flip
  337.     // get the ImageComponent because we need it anyway
  338.     ImageComponent2D imageComp = (ImageComponent2D)tex.getImage(0);
  339.     BufferedImage bImage = imageComp.getImage();
  340.     // convert the image
  341.     bImage = ImageOps.convertImage(bImage, BufferedImage.TYPE_4BYTE_ABGR);
  342.     // flip the image
  343.     ImageOps.flipImage(bImage);
  344.     imageComp.set(bImage);
  345.  
  346.     tex.setCapability(Texture.ALLOW_IMAGE_WRITE);
  347.     tex.setBoundaryModeS(Texture.CLAMP);
  348.     tex.setBoundaryModeT(Texture.CLAMP);
  349.     tex.setBoundaryColor(1.0f, 1.0f, 1.0f, 1.0f);
  350.    
  351.     // set the image of the texture
  352.     tex.setImage(0, imageComp);
  353.  
  354.     // set the texture on the appearance
  355.     appearance.setTexture(tex);
  356.  
  357.     // set texture attributes
  358.     TextureAttributes texAttr = new TextureAttributes();
  359.     texAttr.setTextureMode(TextureAttributes.MODULATE);
  360.     appearance.setTextureAttributes(texAttr);
  361.  
  362.     // set material properties
  363.     Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
  364.     Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
  365.     appearance.setMaterial(new Material(white, black, white, black, 1.0f));
  366.  
  367.     // create a scale transform
  368.     Transform3D scale = new Transform3D();
  369.     scale.set(.6);
  370.     TransformGroup objScale = new TransformGroup(scale);
  371.     objTrans.addChild(objScale);
  372.  
  373.     tetra = new Tetrahedron(true);
  374.     tetra.setAppearance(appearance);
  375.     objScale.addChild(tetra);
  376.  
  377.     // create the behavior
  378.     animate = new AnimateTexturesBehavior(tex, 
  379.                       urls,
  380.                       appearance,
  381.                       this);
  382.     animate.setSchedulingBounds(bounds);
  383.  
  384.     objTrans.addChild(animate);
  385.  
  386.     // add a rotation behavior so we can see all sides of the tetrahedron
  387.       Transform3D yAxis = new Transform3D();
  388.       Alpha rotorAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE,
  389.                        0, 0,
  390.                        4000, 0, 0,
  391.                        0, 0, 0);
  392.       RotationInterpolator rotator =
  393.           new RotationInterpolator(rotorAlpha,
  394.                        objTrans,
  395.                        yAxis,
  396.                        0.0f, (float) Math.PI*2.0f);
  397.       rotator.setSchedulingBounds(bounds);
  398.     objTrans.addChild(rotator);       
  399.  
  400.  
  401.     // have java3d perform optimizations on this scene graph
  402.     objRoot.compile();
  403.  
  404.     return objRoot;
  405.   }
  406.  
  407.   // callback for the animation button and delay text field
  408.   public void actionPerformed(ActionEvent e) {
  409.     Object o = e.getSource();
  410.  
  411.     // for the animation button
  412.     if (o == animationB) {
  413.       if (animate.getEnable()) {
  414.     animate.setEnable(false);
  415.     animationB.setText("start animation");
  416.       }
  417.       else {
  418.     animate.setEnable(true);
  419.     animationB.setText(" stop animation ");
  420.       }
  421.     }
  422.  
  423.     // for the texByRef button
  424.     else if (o == texByRef && texByRef.isSelected()) {
  425.       animate.setByReference(true);
  426.     }
  427.     // texByCopy button
  428.     else if (o == texByCopy && texByCopy.isSelected()) {
  429.       animate.setByReference(false);
  430.     }
  431.     // yUp button
  432.     else if (o == yUp && yUp.isSelected()) {
  433.       animate.setYUp(true);
  434.     }
  435.     // ydown button
  436.     else if (o == yDown && yDown.isSelected()) {
  437.       animate.setYUp(false);
  438.     }
  439.     //geomByRef button
  440.     else if (o == geomByRef) {
  441.       tetra.setByReference(true);
  442.     }
  443.     // geomByCopy button
  444.     else if (o == geomByCopy) {
  445.       tetra.setByReference(false);
  446.     }
  447.     // TYPE_INT_ARGB
  448.     else if (o == imgIntARGB) {
  449.       animate.setImageType(BufferedImage.TYPE_INT_ARGB);
  450.     }
  451.     // TYPE_4BYTE_ABGR
  452.     else if (o == img4ByteABGR) {
  453.       animate.setImageType(BufferedImage.TYPE_4BYTE_ABGR);
  454.     }
  455.     // TYPE_3BYTE_BGR
  456.     else if (o == img3ByteBGR) {
  457.       animate.setImageType(BufferedImage.TYPE_3BYTE_BGR);
  458.     }
  459.     // TYPE_CUSTOM RGBA
  460.     else if (o == imgCustomRGBA) {
  461.       animate.setImageTypeCustomRGBA();
  462.     }
  463.     // TYPE_CUSTOM RGB
  464.     else if (o == imgCustomRGB) {
  465.       animate.setImageTypeCustomRGB();
  466.     }
  467.   }
  468.  
  469.   // callback for the checkboxes
  470.   public void itemStateChanged(ItemEvent e) {
  471.     Object o = e.getSource();
  472.     // for the flip checkbox
  473.     if (o == flipB) {
  474.       if (e.getStateChange() == ItemEvent.DESELECTED) {
  475.     animate.setFlipImages(false);
  476.       }
  477.       else animate.setFlipImages(true);
  478.     }
  479.   }
  480.  
  481.   // callback for the slider
  482.   public void stateChanged(ChangeEvent e) {
  483.     Object o = e.getSource();
  484.     // for the frame delay
  485.     if (o == frameDelay) {
  486.       animate.setFrameDelay(frameDelay.getValue());
  487.     }
  488.   }
  489.  
  490.   // allows TextureByReference to be run as an application as well as an applet
  491.   public static void main(String[] args) {
  492.     java.net.URL fnames[] = null;
  493.     if (args.length > 1) {
  494.       fnames = new java.net.URL[args.length];
  495.       for (int i = 0; i < args.length; i++) {
  496.     try {
  497.       fnames[i] = new java.net.URL("file:" + args[i]);
  498.     }
  499.     catch (java.net.MalformedURLException ex) {
  500.       System.out.println(ex.getMessage());
  501.     }
  502.       }
  503.     }
  504.     else {
  505.       fnames = new java.net.URL[TextureByReference.defaultFiles.length];
  506.       for (int i = 0; i < TextureByReference.defaultFiles.length; i++) {
  507.     try {
  508.       fnames[i] = new java.net.URL("file:" +
  509.                      TextureByReference.defaultFiles[i]);
  510.     }
  511.     catch (java.net.MalformedURLException ex) {
  512.       System.out.println(ex.getMessage());
  513.       System.exit(1);
  514.     }
  515.       }
  516.     }
  517.     new MainFrame((new TextureByReference(fnames)), 650, 750);    
  518.   }
  519. }
  520.  
  521.  
  522.  
  523.  
  524.  
  525.  
  526.